<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="渠道商" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入渠道商"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item label="班次" prop="timeShiftName">
        <el-input
          v-model="queryParams.timeShiftName"
          placeholder="请输入班次"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="班组" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入班组"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工作时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.workTime"
          align="right"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >保存</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
     <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="150" align="center"/>
      <el-table-column label="姓名" prop="workerName" width="150"/>
      <el-table-column label="身份证" prop="idCard" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="联系电话" prop="phoneCode" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="性别" align="center" prop="sex" width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.sex == 1">男</div>
          <div v-else-if="scope.row.sex == 2">女</div>
        </template>
      </el-table-column>
      <el-table-column label="渠道商"  prop="companyName" align="center" width="150"/>
       <el-table-column label="班组"  prop="groupName" align="center" width="150"/>
      <el-table-column label="班次" align="center" prop="timeShiftName"  width="150"/>
       <el-table-column label="上班时间" align="center" prop="signTime"  width="200">
         <template slot-scope="{row,$index}">
           <el-date-picker
             v-model="row.signTime"
             type="datetime"
             placeholder="选择日期时间"
             align="right"
             :picker-options="pickerOptions">
           </el-date-picker>
         </template>
       </el-table-column>
       <el-table-column label="下班时间" align="center" prop="offWorkTime"  width="200">
         <template slot-scope="{row,$index}">
           <el-date-picker
             v-model="row.offWorkTime"
             type="datetime"
             placeholder="选择日期时间"
             align="right"
             :picker-options="pickerOptions">
           </el-date-picker>
         </template>
       </el-table-column>
       <el-table-column label="薪资" align="center" prop="salary"  width="150">
         <template slot-scope="{row,$index}">
           <el-input class="edit-cell" style="width: 70px" v-model="row.salary"/>
         </template>
       </el-table-column>
      <el-table-column label="工作时间" align="center" prop="workTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTime,"{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="200"/>
    </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listAttendance,savaAttendanceInfos,exportAttendance} from "@/api/job/attendance/attendanceApi";
import { getToken } from "@/utils/auth";
export default {
  name: "attendanceGroup",
  created() {
   this.getTime ();
    this.getList();

  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "工具类导入",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
         url: process.env.VUE_APP_BASE_API + "/excelToolApi/importDataCompany"
      },
      savaDatas:[],
      signUpLoading:true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      signUp:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //渠道名称
        companyName: undefined,
        timeShiftName:undefined,
        groupName:undefined,
        //联系人
        workerName: undefined,
        workTime:undefined
      },
      timeShiftList:[],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
    };
  },
  methods: {
    getTime () {
      let _this = this;
      let date = new Date();
      let y = date.getFullYear();
      let m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1);
      let d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();

      let start_month = y + '-' + '01';   //开始月份：本年第一月份
      let end_month = `${y}-${m}`;        //结束月份： 本年当前月份

      let start_day = `${y}-${m}-${d}`;       //开始日期： 本月第一天
      let end_day = `${y}-${m}-${d}`;     //结束日期： 本月今天

      //_this.aa = [start_month, end_month];   //默认月份区间
      _this.queryParams.workTime  = start_day;        //默认日期区间
    },
    /** 获取员工信息列表 */
    getList() {
      this.loading = true;
      listAttendance(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.attendanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 表单重置
    reset() {
        this.form = {
          id:undefined,
          groupName: undefined,
          workTime: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "班组信息导入";
      this.upload.open = true;
    },
    handleSelectionChange(selection) {
      this.savaDatas = selection.map(item=>item)
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleExport(){
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有员工数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAttendance(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    handleAdd() {
      if (this.savaDatas.length == 0 ) {
        this.$alert("请选择需要保存的数据", "保存错误");
      }else {
        savaAttendanceInfos(this.savaDatas).then(res=>{
          this.msgSuccess( "数据保存成功");
        }).catch(()=>{
          this.msgError("保存失败");
        })
      }
    }
  }
};
</script>
